package com.huatai.manage.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.manage.aop.annotation.TrialIdIsExist;
import com.huatai.manage.query.PageQuery;
import com.huatai.manage.query.RemoveQuery;
import com.huatai.manage.entity.TbTrialSchemeEntity;

import com.huatai.manage.query.TrialIdQuery;
import com.huatai.manage.service.TbTrialSchemeService;
import com.huatai.manage.utils.ComUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 模板表; 前端控制器
 * </p>
 *
 * @author luoxc
 * @since 2023-03-16
 */
@RestController
@RequestMapping("scheme")
@Api(value = "方案管理（母版）", tags = "方案管理（母版）")
public class TbTrialSchemeController {


	@Autowired
	private TbTrialSchemeService tbTrialSchemeService;

	@ApiOperationSupport(order = 1, includeParameters = {
		"pageQuery.current",
		"pageQuery.size",
		"pageQuery.data.name",
		"pageQuery.data.type",
		"pageQuery.data.trialId"
	})
	@PostMapping("pageList")
	@ApiOperation(value = "分页查询所有数据", notes = "分页查询所有数据")
	public R selectAll(@RequestBody PageQuery<TbTrialSchemeEntity> pageQuery) {
		return R.data(tbTrialSchemeService.pageList(pageQuery));
	}

	@GetMapping("list")
	@ApiOperation(value = "查询所有数据", notes = "查询所有数据")
	public R list() {
		return R.data(this.tbTrialSchemeService.schemeList());
	}

	@ApiOperationSupport(order = 1, includeParameters = {
		"pageQuery.current",
		"pageQuery.size",
		"pageQuery.data.trialId",
	})
	@PostMapping("pageByTrialId")
	@ApiOperation(value = "根据试验id分页查询面案列表", notes = "根据试验id分页查询面案列表")
	public R listByTrialId(@RequestBody PageQuery<TbTrialSchemeEntity> pageQuery) {
		return R.data(this.tbTrialSchemeService.pageByTrialId(pageQuery));
	}

	@PostMapping("listByTrialId")
	@ApiOperation(value = "根据试验id查询面案列表", notes = "根据试验id查询面案列表")
	public R listByTrialId(@ApiParam(name = "trialId", value = "试验id", required = true) @RequestParam("trialId") String trialId) {
		return R.data(this.tbTrialSchemeService.listByTrialId(trialId));
	}

	@PostMapping("getByTrialId")
	@ApiOperation(value = "根据试验id查询（壹瞰思）", notes = "根据试验id查询（壹瞰思）")
	public R getByTrialId(@RequestBody @Valid TrialIdQuery trialIdQuery) {
		return R.data(this.tbTrialSchemeService.getByTrialId(trialIdQuery.getTrialId()));
	}

	@PostMapping("getBySchemeId")
	@ApiOperation(value = "通过方案id查询单条数据", notes = "通过方案id查询单条数据")
	public R getOne(@ApiParam(name = "schemeId", value = "方案id", required = true) @RequestParam("schemeId") String schemeId) {
		return R.data(this.tbTrialSchemeService.getBySchemeId(schemeId));
	}


	@TrialIdIsExist(remark = "校验trialId在数据库中是否存在")
	@PostMapping("insert")
	@ApiOperation(value = "新增数据", notes = "新增数据")
	public R insert(@RequestBody @Valid TbTrialSchemeEntity tbTrialSchemeEntity) {
		return R.status(this.tbTrialSchemeService.insert(tbTrialSchemeEntity.setSchemeId(ComUtil.getUUID())));
	}

	@TrialIdIsExist(remark = "校验trialId在数据库中是否存在")
	@PostMapping("update")
	@ApiOperation(value = "修改数据", notes = "修改数据")
	public R update(@RequestBody @Valid TbTrialSchemeEntity tbTrialSchemeEntity) {
		return R.status(this.tbTrialSchemeService.update(tbTrialSchemeEntity));
	}


	@PostMapping("removes")
	@ApiOperation(value = "删除数据", notes = "删除数据")
	public R removes(@RequestBody RemoveQuery remove) {
		return R.status(this.tbTrialSchemeService.removes(remove.getIds()));
	}

}
